<?php
require_once "./Base.php";


use PhpAmqpLib\Connection\AMQPStreamConnection;

class Consume extends Base
{
//    消费队列
    public function run()
    {
        $exchangeName = 'liu_qiang_test'; //交换机名
//        $queueName = 'liu_qiang_test_queue'; //队列名称
        $queueName = 'liu_qiang_late'; //队列名称
        $routingKey = 'liu_qiang'; //路由关键字(也可以省略)
        //定义回调函数
        $callback = function ($msg) {
            $this->callbackMsg($msg);
        };
        //告诉rabbitmq 一次不要发送过多消息 处理完了在发送
        $this->channel->basic_qos(null, 1, null);
        //获取消费消息
        $this->channel->basic_consume($queueName, '', false, true, false, false, $callback);
        //循环消费
        while (count($this->channel->callbacks)) {
            $this->channel->wait();
        }
        $this->close();//关闭链接
    }

    /**
     *
     * @param $msg PhpAmqpLib\Message\AMQPMessage
     *
     */
    public function callbackMsg($msg)
    {
        $res = json_decode($msg->body, true);
        $res['test']= mt_rand();
        var_dump($res);
    }
}

$obj = new Consume();
$obj->run();
